#include <Rectangle.h>

Rectangle::Rectangle(float width, float height)
{
	if ( ! isValid(width) || ! isValid(height))
	{
		m_width = m_height = 0;
		return;
	}

	m_width = truncate(width);
	m_height = truncate(height);
}

float Rectangle::getArea() const
{
	return float((int(m_width * m_height * 100 + 0.5)) / 100.0);
}

float Rectangle::getPerimeter() const
{
	return float((int(2 * (m_width + m_height) * 100 + 0.5)) / 100.0);
}

bool Rectangle::isValid(float len) const
{
	return (( len > 0) && (len <= MAX_LEN));
}

bool Rectangle::moreDecimal(float len) const
{
	stringstream ss (stringstream::in | stringstream::out); 

	ss << len;

	string str = ss.str();

	int loc = str.find('.');

	if ((-1 != loc) && ( 4 <= (str.size() - loc)))
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool Rectangle::setSideLen(float width, float height)
{
	if ( ! isValid(width) || ! isValid(height))
	{
		return false;
	}
	else
	{
		m_width = truncate(width);
		m_height = truncate(height);

		return true;
	}
}

float Rectangle::getRoundup(float len) const
{
	return float((int(len * 100 + 0.5)) / 100.0);
}

float Rectangle::truncate(float len) 
{
	if (moreDecimal(len))
	{
		return float((int(len * 100)) / 100.0);
	}
	else
	{
		return len;
	}
}

